// 获取应用实例
var Util = require('../../utils/util.js')
const app = getApp()
Page({
  data: {
    vcode: '',
    telphone: '',
    codeText: "获取验证码",
    isDisabled: false
  },
  // 倒计时120s
  setTimeFun() {
    var _this = this;
    var countdown = 120;
    setTime();

    function setTime() {
      if (countdown == 0) {
        _this.setData({
          isDisabled: false,
          codeText: "获取验证码"
        })
        countdown = 120;
        return false;
      } else {
        _this.setData({
          isDisabled: true,
          codeText: `重新发送(${countdown})`
        })
        countdown--;
      }
      setTimeout(() => {
        setTime();
      }, 1000);
    }
  },
  test() {



  },
  // 获取验证码
  getCode: function () {
    var _this = this;
    let telphone = _this.data.telphone
    let reg = /^1(([378][0-9])|(5[^4,\D])|(4[579])|(9[89])|(66))\d{8}$/;

    if (!(reg.test(telphone))) {
      wx.showToast({
        title: '请输入有效手机号码',
        icon: 'error'
      })
      return false
    }
    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType
        console.log(res);
        if ("none" == networkType) {
          wx.showLoading({
            title: '发送失败，请检查网络',
            mask: true
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 3000)
        } else {
          var sign = Util.encryptionParam(telphone, 'login');
          wx.request({
            url: 'https://api.jiudaifu.com/v2/user/makeJLXWVerifyCode',
            data: {
              type: 'login',
              sign: sign,
              version: 'v1',
              mobile: telphone
            },
            method: 'GET',
            success: function (res) {
              wx.showToast({
                title: '发送成功',
                icon: 'success',
                duration: 1000
              })
              _this.setTimeFun();
            }
          })
        }
      },
      fail(err) {
        console.log(err)
      },
      complete(cpe) {
        console.log(cpe)
      }
    })
  },
  // 手机验证码登录
  login() {
    
    let that = this;
    let telphone = that.data.telphone
    let reg1 = /^1(([378][0-9])|(5[^4,\D])|(4[579])|(9[89])|(66))\d{8}$/;
    let vccode = that.data.vcode
    let reg = /^\d{6}$/;

    if (!(reg1.test(telphone)) && !(reg.test(vccode))) {
      wx.showToast({
        title: '请输入有效手机',
        icon: 'error'
      })
      return false
    }else if((reg1.test(telphone)) && !(reg.test(vccode))){
      wx.showToast({
        title: '无效验证码',
        icon: 'error'
      })
      return false
    }
     else {
      wx.getSystemInfo({
        success(res) {
          wx.request({
            url: 'https://api.jiudaifu.com/v2/user/login',
            data: {
              type: 'vcode',
              uid: '',
              p: '',
              openid: '',
              mobile: that.data.telphone,
              vcode: that.data.vcode,
              dev: res.model
            },
            method: 'GET',
            success: function (res) {
              console.log("登录成功返回数据", res.data);
              let obj = {
                uid: res.data.uid,
                token: res.data.token
              }
              let token = JSON.stringify(obj)
              wx.setStorageSync("token", token);  
              console.log(token)
           
               if (res.data.error != 0  || !(reg.test(vccode))) {
                wx.showToast({
                  title: '验证码错误',
                  icon: 'error'
                })   
                return false
              } else {
                wx.switchTab({
                  url: '/pages/index/index',
                })  
              }
            },
            fail: function (res) {
              console.log(res, '-------------------------------')
            }
          })
        }
      });
    }



  },
  // 监听并保存手机 验证码
  vcodeOnchange: function (event) {
    var code = event.detail.value;
    this.setData({
      vcode: code
    });
  },
  moblieOnchange: function (event) {
    var tel = event.detail.value;
    this.setData({
      telphone: tel
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {

   
	
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    // wx.showModal({
    //   title: '温馨提示',
    //   content: '正在请求您的个人信息',
    //   success(res) {
    //     if (res.confirm) {
    //       wx.getUserProfile({
    //       desc: "获取你的昵称、头像、地区及性别",
    //       success: res => {
    //         console.log(res)
    //         let wxUserInfo = res.userInfo;
    //       },
    //       fail: res => {
    //          //拒绝授权
    //         that.showErrorModal('您拒绝了请求');
    //         return;
    //       }
    //     })} else if (res.cancel) {
    //       //拒绝授权 showErrorModal是自定义的提示
    //       that.showErrorModal('您拒绝了请求');
    //       return;
    //     }
    //   }
    // })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(111)
    var that = this;
    var goods_id = that.data.goods_id; //获取产品id
    var goods_title = that.data.goods_title; //获取产品标题
    var goods_img = that.data.goods_img; //产品图片
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: goods_title,
        path: '/page/details?id=' + goods_id,
        imageUrl: goods_img //不设置则默认为当前页面的截图
      }
    }
  }
})